home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / programming / c / supralib / developer / source.org / addtooltype.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  6KB  |  207 lines

  1. /****** AddToolType *****************************************************
  2. *
  3. *   NAME
  4. *       AddToolType -- Adds or changes a new/existing icon's tooltype (V10)
  5. *       (icon)
  6. *
  7. *   SYNOPSIS
  8. *       tool = AddToolType(diskobj, tooltype)
  9. *
  10. *       char * = AddToolType(struct DiskObject *, char *);
  11. *
  12. *   FUNCTION
  13. *       This function lets you add a new tooltype to a disk object's
  14. *       tooltype list, or change already existing one.
  15. *       It is a smart routine that makes dealing with tooltypes very
  16. *       straightforward.
  17. *
  18. *       The following is an example table about how a tooltype list gets
  19. *       changed based on a provided tool:
  20. *
  21. *        existing tooltype | provided tooltype | result
  22. *       ----------------------------------------------------------
  23. *            NOGUI         |     (NOGUI)       | (NOGUI)
  24. *            (NOGUI)       |     NOGUI         | NOGUI
  25. *            NOGUI         |     NOGUI         | NOGUI
  26. *            SIZE=10       |     SIZE=15       | SIZE=15
  27. *            (SIZE=10)     |     SIZE=15       | SIZE=15
  28. *            SIZE=10       |     (SIZE=15)     | (SIZE=15)
  29. *            [a new one]   |     DONOTWAIT     | DONOTWAIT [added to a list]
  30. *
  31. *
  32. *   INPUTS
  33. *       diskobj - points to an allocated DiskObject structure (usually
  34. *                 created by GetDiskObject() function).
  35. *
  36. *       tooltype - points to a new tooltype string to be added to a
  37. *                  provided tooltype list
  38. *
  39. *   RESULT
  40. *       tool = pointer to a provided tooltype string if succeeds, otherwise
  41. *       NULL.
  42. *
  43. *   EXAMPLES
  44. *       This example opens a ram:test.info icon and asks a user to enter
  45. *       tooltypes to be added (until user enters 'end').
  46. *
  47. *
  48. *   #include <libraries/supra.h>
  49. *   #include <clib/exec_protos.h>
  50. *   #include <clib/dos_protos.h>
  51. *   #include <clib/icon_protos.h>
  52. *   #include <stdio.h>
  53. *   #include <string.h>
  54. *
  55. *   #define filename "ram:test"
  56. *
  57. *   struct Library *IconBase = NULL;
  58. *
  59. *   struct DiskObject *diskobj;
  60. *
  61. *   char icon[50];
  62. *
  63. *   main()
  64. *   {
  65. *       key = NULL;
  66. *           if (IntuitionBase = OpenLibrary("intuition.library",0)) {
  67. *               if (IconBase = OpenLibrary("icon.library",0)) {
  68. *                   if (diskobj = GetDiskObject(filename)) {
  69. *                       do {
  70. *                           gets(icon);
  71. *                           if (strcmp(icon, "end") == 0) break;
  72. *                           AddToolType(diskobj, icon);
  73. *                       } while (TRUE);
  74. *
  75. *                       PutDiskObject(filename, diskobj);
  76. *                       FreeDiskObject(diskobj);
  77. *                       FreeRemember(&key, TRUE);
  78. *                   }
  79. *               }
  80. *           }
  81. *
  82. *       if (IconBase) CloseLibrary(IconBase);
  83. *       if (IntuitionBase) CloseLibrary(IntuitionBase);
  84. *   }
  85. *
  86. *
  87. *   NOTES
  88. *       All memory allocations used by AddToolType() will be stored in
  89. *       FreeList structure that MUST be allocated right after the provided
  90. *       DiskObject structure. If you called your DiskObject by GetDiskObject()
  91. *       or GetDiskObjectNew() then FreeList is automaticly appended.
  92. *       All allocated memory is freed when you call FreeDiskObject().
  93. *
  94. *       This function requires icon.library to be opened.
  95. *
  96. ****************************************************************************/
  97.  
  98. #include <proto/exec.h>
  99. #include <proto/intuition.h>
  100. #include <proto/icon.h>
  101. #include <workbench/workbench.h>
  102. #include <stdlib.h>
  103. #include <string.h>
  104.  
  105. /* This compares a tooltype entry to a string
  106.  * Returns: 0 if absolutely the same
  107.  *          1 if string after '=' is different
  108.  *          2 if different
  109.  *          3 if different only in brackets
  110.  *          4 if string after '=' is different, and dif. in brackets
  111.  *          5 different in all (same as 2)
  112.  */
  113. int ToolMatch(char *s1, char *s2)
  114. {
  115. int cmp = 2;
  116. int offs = 0;
  117.  
  118.     if ((*s1 == '(' || *s2 == '(') && *s1 != *s2) offs = 3;
  119.     if (*s1 == '(') s1++;
  120.     if (*s2 == '(') s2++;
  121.     if (*s1 == ')') {
  122.             if (*(s1+1) == '\0') s1++;
  123.     }
  124.  
  125.     if (*s2 == ')') {
  126.         if (*(s2+1) == '\0') s2++;
  127.     }
  128.  
  129.  
  130.     while (*s1 && *s2) {
  131.         if (*s1 == '\0' && *s2 == '\0') return(0+offs);
  132.         if (*s1 != *s2) return(cmp+offs);
  133.         if (*s1 == '=') cmp=1;
  134.         s1++; s2++;
  135.         if (*s1 == ')') {
  136.             if (*(s1+1) == '\0') s1++;
  137.         }
  138.  
  139.         if (*s2 == ')') {
  140.             if (*(s2+1) == '\0') s2++;
  141.         }
  142.  
  143.     }
  144.  
  145.     if (*s1 == '\0' && *s2 == '\0') return(0+offs);
  146.     else return(cmp+offs);
  147. }
  148.  
  149. char *AddToolType(struct DiskObject *dobj, char *tool)
  150. {
  151. struct FreeList *flist = (struct FreeList *)dobj + sizeof(struct DiskObject);
  152. char ***ttypes = &(dobj->do_ToolTypes);
  153. char *item = (*ttypes)[0];
  154. int change = -1;
  155. int i=0;
  156. char *newmem;
  157. int len;
  158.  
  159.     while (item != NULL) {
  160.       if (change == -1) { /* We haven't found any matching tooltype yet */
  161.         switch(ToolMatch(item, tool)) {
  162.             case 0: return(item);   /* This tooltype already defined */
  163.             case 1:
  164.             case 3:
  165.             case 4: change = i;     /* Change this tooltype */
  166.         }   /* 2,5 means different */
  167.       }
  168.       i++;
  169.       item = (*ttypes)[i]; /* Get the next tooltype to be examined */
  170.     }
  171.  
  172.     if (change != -1) {
  173.         len = strlen(tool)+1;
  174.         if ((newmem = AllocMem(len, 0L)) == NULL) return(NULL);
  175.         else if (AddFreeList(flist, newmem, len) == FALSE) {
  176.             FreeMem(newmem, len);
  177.             return(NULL);
  178.         }
  179.  
  180.         strcpy(newmem, tool);
  181.         (*ttypes)[change] = newmem;
  182.         return(newmem);
  183.     } else {
  184.         len = (i+2)*4;
  185.         if ((newmem = AllocMem(len, 0L)) == NULL) return(NULL);
  186.         else if (AddFreeList(flist, newmem, len) == FALSE) {
  187.             FreeMem(newmem, len);
  188.             return(NULL);
  189.         }
  190.  
  191.         memcpy(newmem, *ttypes, len);
  192.         *ttypes = (char **)newmem;
  193.  
  194.         len = strlen(tool)+1;
  195.         if ((newmem = AllocMem(len, 0L)) == NULL) return(NULL);
  196.         else if (AddFreeList(flist, newmem, len) == FALSE) {
  197.             FreeMem(newmem, len);
  198.             return(NULL);
  199.         }
  200.  
  201.         strcpy(newmem, tool);
  202.         (*ttypes)[i] = newmem;
  203.         (*ttypes)[i+1] = NULL;
  204.         return(newmem);
  205.     }
  206. }
  207.